{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Other useful tools\n",
    "\n",
    "**Prabhu Ramachandran**\n",
    "\n",
    "**Department of Aerospace Engineering, IIT Bombay**\n",
    "<br/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- The `mayavi2` application\n",
    "    - Features\n",
    "    - Command line arguments\n",
    "    - `mlab` integration\n",
    "\n",
    "- Jupyter notebooks\n",
    "\n",
    "- Offscreen support\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Introduction to the application\n",
    "\n",
    "* Visualization without scripting\n",
    "\n",
    "* Especially useful with data files\n",
    "\n",
    "* Can be scripted\n",
    "\n",
    "* Supports `mlab`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<img height=\"100%\" src=\"MEDIA/m2/m2_app3_3.png\"/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Features\n",
    "\n",
    "* Complete UI\n",
    "* Powerful command line options\n",
    "* Embedded Python\n",
    "* Fully scriptable\n",
    "* Reads different file formats\n",
    "* Drag-drop onto shell\n",
    "* Script recording\n",
    "* Extensible\n",
    "* Plug-in design using Envisage\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Command line arguments\n",
    "### Overview\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mayavi2 -h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* `-d datafile.ext` : Load data file\n",
    "\n",
    "* `-f FilterName` : Load filter\n",
    "\n",
    "* `-m ModuleName` : Load module\n",
    "\n",
    "* `-s python-string` : Evaluate Python expression\n",
    "\n",
    "* `-x file.py` : Execute Python code\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Advanced options\n",
    "\n",
    "* `-n` : new scene\n",
    "\n",
    "* `-M` : new module manager\n",
    "\n",
    "* `-o` : offscreen mode\n",
    "\n",
    "* `-z filename` : load saved visualization\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Simple example\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mayavi2 -d room_vis.wrl -d fire_ug.vtu -m Outline  -m ScalarCutPlane"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Scripting from the command line\n",
    "\n",
    "* `-s python-code`  is very powerful\n",
    "\n",
    "* Takes Python expressions\n",
    "\n",
    "* Applies to the last module/filter\n",
    "\n",
    "* Can be called multiple times\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## An example\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mayavi2 -d room_vis.wrl -d fire_ug.vtu \\\n",
    " -m Outline  -m ScalarCutPlane \\\n",
    "-s \"enable_contours = True\" \\\n",
    "-s \"implicit_plane.widget.normal_to_z_axis=1\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## `mlab` integration\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Using `mlab`\n",
    "\n",
    "* Can import and use mlab\n",
    "\n",
    "* Opening data: `mlab.pipeline.open`\n",
    "* Add dataset: `mlab.pipeline.add_dataset`\n",
    "* Use `lower_case_with_underscores`\n",
    "* So `ScalarCutPlane`  $\\rightarrow$ `mlab.pipeline.scalar_cut_plane`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- The `mayavi2` application\n",
    "    - Command line arguments\n",
    "    - UI features\n",
    "    - `mlab` integration\n",
    "\n",
    "- **Jupyter notebooks** $\\Longleftarrow$\n",
    "\n",
    "- Offscreen support\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Inline IPython support\n",
    "Installation/setup:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$ pip install ipyevents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$ jupyter nbextension install --py mayavi --user"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Usage:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mayavi import mlab\n",
    "mlab.init_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = mlab.test_plot3d()\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## See full demo\n",
    "\n",
    "- Works with notebooks and jupyterlab\n",
    "- `notebooks/mayavi_jupyter.ipynb`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- The `mayavi2` application\n",
    "    - Command line arguments\n",
    "    - UI features\n",
    "    - `mlab` integration\n",
    "\n",
    "- Jupyter notebooks\n",
    "\n",
    "- **Offscreen support** $\\Longleftarrow$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Offscreen rendering\n",
    "\n",
    "* Depends on hardware, OS, build\n",
    "\n",
    "* Works best on Windows!\n",
    "\n",
    "* Basics are easy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mayavi import mlab\n",
    "mlab.options.offscreen = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.test_plot3d()\n",
    "mlab.savefig('/tmp/test.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## What next?\n",
    "\n",
    "- Learn more about the different sources, filters, and modules\n"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "livereveal": {
   "controls": true,
   "help": true,
   "scroll": true,
   "slideNumber": true,
   "transition": "none"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
